package com.example.tiantian.contact;

import com.example.tiantian.BaseActivity;
import com.example.tiantian.R;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;

public class ContactInfoActivity extends BaseActivity implements OnClickListener, OnPageChangeListener {

    public static final String BACK_RESULT = "contact";

    public static final int RESULT_ADD = 2;
    public static final int RESULT_UPDATE = 1;
    public static final int RESULT_NOTHING = 0;

    ImageView imgIcon;
    EditText txtName;
    EditText txtMobile;
    EditText txtOffice;
    EditText txtfamily;
    EditText txtPosition;
    EditText txtCompany;
    EditText txtAddress;
    EditText txtZipcode;
    EditText txtEmail;
    EditText txtOthercontact;
    EditText txtRemark;
    Button btn1;
    Button btnBack;

    DBHelper helper;

    int state;

    ContactInfo info;

    AlertDialog iconChanger;

    int[] icons;
    int indexSelected;

    IconAdapter adapter;

    ViewPager view;

    @Override
    protected void init() {
        setContentView(R.layout.activity_contact);

        imgIcon = (ImageView) this.findViewById(R.id.contact_info_icon);
        txtName = (EditText) this.findViewById(R.id.contact_info_name);
        txtMobile = (EditText) this.findViewById(R.id.contact_info_phone_moblie);
        txtOffice = (EditText) this.findViewById(R.id.contact_info_phone_office);
        txtfamily = (EditText) this.findViewById(R.id.contact_info_phone_family);
        txtCompany = (EditText) this.findViewById(R.id.contact_info_company);
        txtPosition = (EditText) this.findViewById(R.id.contact_info_position);
        txtAddress = (EditText) this.findViewById(R.id.contact_info_address);
        txtEmail = (EditText) this.findViewById(R.id.contact_info_email);
        txtZipcode = (EditText) this.findViewById(R.id.contact_info_zipcode);
        txtOthercontact = (EditText) this.findViewById(R.id.contact_info_other);
        txtRemark = (EditText) this.findViewById(R.id.contact_info_remark);
        btn1 = (Button) this.findViewById(R.id.contact_info_btn);
        btnBack = (Button) this.findViewById(R.id.contact_info_back);
        imgIcon.setImageResource(R.drawable.image1);// 设置头像的默认图标
        this.helper = DBHelper.getHelper(this);
        state = getIntent().getIntExtra(ContactActivity.REQUEST, ContactActivity.REQUEST_CODE_ADD);
        switch (state) {
        case ContactActivity.REQUEST_CODE_EDIT:
            btn1.setText(R.string.save);
            break;
        case ContactActivity.REQUEST_CODE_SHOW:
            btn1.setText(R.string.edit);
            lock();
        default:
            break;
        }
    }

    @Override
    protected void loadData() {
        if (state != ContactActivity.REQUEST_CODE_ADD) {
            info = getIntent().getParcelableExtra(ContactActivity.CONTACTINFO);
            imgIcon.setImageResource(info.imageid);

            txtName.setText(info.name);
            txtMobile.setText(info.mobilephone);
            txtOffice.setText(info.officephone);
            txtfamily.setText(info.familyphone);
            txtCompany.setText(info.company);
            txtPosition.setText(info.position);
            txtAddress.setText(info.address);
            txtEmail.setText(info.email);
            txtZipcode.setText(info.zipcode);
            txtOthercontact.setText(info.othercontact);
            txtRemark.setText(info.remark);
        }
        TypedArray ta = getResources().obtainTypedArray(R.array.contact_icon);
        icons = new int[ta.length()];
        for (int i = 0; i < icons.length; i++) {
            icons[i] = ta.getResourceId(i, R.drawable.image1);
            if (info != null && icons[i] == info.imageid) {
                indexSelected = i;
            }
        }
        ta.recycle();
        adapter = new IconAdapter();
        iconChanger = builderChanger();
    }

    @Override
    protected void event() {
        btn1.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        imgIcon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.contact_info_btn:
            // 返回 同时返回一个联系人对象 到主界面
            Intent intent_back = new Intent(this, ContactActivity.class);
            switch (state) {
            case ContactActivity.REQUEST_CODE_ADD:
                long id = helper.add(getContact());
                info._id = id;
                // 将联系人对象 绑定上 intent
                setResult(RESULT_ADD, intent_back.putExtra(BACK_RESULT, info));
                toast(info.getName() + getString(R.string.contact_info_added));
                finish();
                break;
            case ContactActivity.REQUEST_CODE_EDIT:
                helper.update(getContact());
                // 将联系人对象 绑定上 intent
                setResult(RESULT_UPDATE, intent_back.putExtra(BACK_RESULT, info));
                toast(info.getName() + getString(R.string.contact_info_updated));
                finish();
                break;
            case ContactActivity.REQUEST_CODE_SHOW:
                state = ContactActivity.REQUEST_CODE_EDIT;
                btn1.setText(R.string.save);
                unlock();
                break;
            default:
                break;
            }
            break;
        case R.id.contact_info_back:
            switch (state) {
            case ContactActivity.REQUEST_CODE_ADD:
                toast(getString(R.string.contact_info_add_cancel));
                break;
            case ContactActivity.REQUEST_CODE_EDIT:
                toast(getString(R.string.contact_info_update_cancel));
                finish();
                break;
            default:
                break;
            }
            finish();
            break;
        case R.id.contact_info_icon:
            iconChanger.show();
            break;
        default:
            break;
        }
    }

    private void lock() {
        imgIcon.setEnabled(false);

        txtName.setEnabled(false);
        txtMobile.setEnabled(false);
        txtOffice.setEnabled(false);
        txtfamily.setEnabled(false);
        txtCompany.setEnabled(false);
        txtPosition.setEnabled(false);
        txtAddress.setEnabled(false);
        txtEmail.setEnabled(false);
        txtZipcode.setEnabled(false);
        txtOthercontact.setEnabled(false);
        txtRemark.setEnabled(false);
    }

    private void unlock() {
        imgIcon.setEnabled(true);

        txtName.setEnabled(true);
        txtMobile.setEnabled(true);
        txtOffice.setEnabled(true);
        txtfamily.setEnabled(true);
        txtCompany.setEnabled(true);
        txtPosition.setEnabled(true);
        txtAddress.setEnabled(true);
        txtEmail.setEnabled(true);
        txtZipcode.setEnabled(true);
        txtOthercontact.setEnabled(true);
        txtRemark.setEnabled(true);
    }

    private ContactInfo getContact() {
        if (info == null) {
            info = new ContactInfo();
        }
        info.imageid = icons[indexSelected];
        info.name = txtName.getText().toString();
        info.mobilephone = txtMobile.getText().toString();
        info.familyphone = txtfamily.getText().toString();
        info.officephone = txtOffice.getText().toString();
        info.address = txtAddress.getText().toString();
        info.company = txtCompany.getText().toString();
        info.position = txtPosition.getText().toString();
        info.zipcode = txtZipcode.getText().toString();
        info.email = txtEmail.getText().toString();
        info.othercontact = txtOthercontact.getText().toString();
        info.remark = txtRemark.getText().toString();

        return info;
    }

    private AlertDialog builderChanger() {
        Builder builder = new Builder(this);

        view = new ViewPager(this);
        view.setAdapter(adapter);
        LayoutParams params = new LayoutParams();
        params.height = LayoutParams.WRAP_CONTENT;
        params.width = LayoutParams.MATCH_PARENT;
        view.setLayoutParams(params);
        builder.setTitle(R.string.contact_info_select_icon).setView(view).setPositiveButton(getString(R.string.ok),
                null);
        view.setOnPageChangeListener(this);
        view.setOnClickListener(this);

        view.setCurrentItem(indexSelected);

        return builder.create();
    }

    class IconAdapter extends PagerAdapter {

        ImageView[] list;
        LayoutParams params;

        public IconAdapter() {
            list = new ImageView[icons.length];
            params = new LayoutParams();
            params.height = LayoutParams.WRAP_CONTENT;
            params.width = LayoutParams.MATCH_PARENT;
        }

        @Override
        public int getCount() {
            return icons.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list[position]);
        }

        @Override
        public ImageView instantiateItem(ViewGroup container, int position) {
            if (list[position] == null) {
                list[position] = new ImageView(ContactInfoActivity.this);
                list[position].setImageResource(icons[position]);
                list[position].setLayoutParams(params);
            }
            container.addView(list[position]);
            return list[position];
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // Do nothing
    }

    @Override
    public void onPageSelected(int position) {
        indexSelected = position;

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        imgIcon.setImageResource(icons[indexSelected]);
    }
}
